package com.urbanski.clipboard.server;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import com.urbanski.clipboard.common.message.Response;

public class ClientSocket
{
	private Socket socket;
	private String clientName;
	private ObjectInputStream inputStream;
	private ObjectOutputStream outputStream;
	
	public ClientSocket(Socket socket, String clientName, ObjectInputStream inputStream) throws IOException
	{
		this.socket = socket;
		this.clientName = clientName;
		this.inputStream = inputStream;
		this.outputStream = new ObjectOutputStream(socket.getOutputStream());
	}

	public String getClientName()
	{
		return clientName;
	}

	public void setClientName(String clientName)
	{
		this.clientName = clientName;
	}

	public Socket getSocket()
	{
		return socket;
	}

	public ObjectInputStream getInputStream()
	{
		return inputStream;
	}

	public ObjectOutputStream getOutputStream()
	{
		return outputStream;
	}
	
	public void close()
	{
		try
		{
			socket.close();
		}
		catch (IOException e)
		{
			System.err.println("Closing socket: ");
			e.printStackTrace();
		}
	}

	public void sendResponse(Response response) throws IOException
	{
		outputStream.writeObject(response);
		outputStream.flush();
	}
	
	public String toString()
	{
		return clientName + " (" + socket.getInetAddress().getHostName() + ')';
	}
}
